package com.manopservice.dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.manopservice.model.CarBrand;
import com.manopservice.model.CarModel;

@Repository("carModelDAO")
public class CarModelDAOImpl implements CarModelDAO{
	
	@Autowired
	private SessionFactory sessionFactory;

	@Override
	public void save(CarModel carModel) {
		sessionFactory.getCurrentSession().saveOrUpdate(carModel);
		
	}
	
	@Override
	public void delete(int id) {
		CarModel carModel = (CarModel) sessionFactory.getCurrentSession().get(CarModel.class, id);
		sessionFactory.getCurrentSession().delete(carModel);	
	}

	@SuppressWarnings("unchecked")
	public List<CarModel> findAll() {
		
		return (List<CarModel>) sessionFactory.getCurrentSession().createCriteria(CarModel.class).list();
	}

	@Override
	public CarModel findById(int id) {
		
		return (CarModel) sessionFactory.getCurrentSession().get(CarModel.class, id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CarModel> findByCarBrand(CarBrand carBrand) {	
		
		return (List<CarModel>) sessionFactory.getCurrentSession().createCriteria(CarModel.class).add(Restrictions.eq("carBrand", carBrand)).list();
	}

}
